﻿ class D3D.Vertex
{
	public var x : Number;
	public var y : Number;
	public var z : Number;
	public var w : Number;
	public function Vertex (xx, yy, zz, ww)
	{
		if (xx == undefined || xx == "NaN")
		{
			xx = 0;
		}
		if (yy == undefined || yy.toString () == "NaN")
		{
			yy = 0;
		}
		if (zz == undefined || zz.toString () == "NaN" )
		{
			zz = 0;
		}
		if (ww == undefined || ww.toString () == "NaN")
		{
			ww = 1;
		}
		x = xx;
		y = yy;
		z = zz;
		w = ww;
	}
	/** 点位置的减法
	*
	* @usage
	* @param   vertex 被减点
	* @return  		新的点
	*/
	public function deduct (vertex : Vertex) : Vertex
	{
		if (vertex != undefined && vertex.x != undefined)
		{
			if (w != 1)
			{
				x = x / w;
				y = y / w;
				z = z / w;
				w = 1;
			}
			if (vertex.w != w)
			{
				vertex.x = vertex.x / vertex.w;
				vertex.y = vertex.y / vertex.w;
				vertex.z = vertex.z / vertex.w;
				vertex.w=1;
			}
			var rv:Vertex=new Vertex();
			rv.x=x-vertex.x;
			rv.y=y-vertex.y;
			rv.z=z-vertex.z;
			rv.w=1;
			return rv;
		}else{
			say("减法参数错误");
		}
	}
	/**  点位置的加法
	*
	* @usage
	* @param   vertex 被加点
	* @return  		新的点
	*/
	public function plus (vertex : Vertex) : Vertex
	{
		if (vertex != undefined && vertex.x != undefined)
		{
			if (w != 1)
			{
				x = x / w;
				y = y / w;
				z = z / w;
				w = 1;
			}
			if (vertex.w != w)
			{
				vertex.x = vertex.x / vertex.w;
				vertex.y = vertex.y / vertex.w;
				vertex.z = vertex.z / vertex.w;
				vertex.w=1;
			}
			var rv:Vertex=new Vertex();
			rv.x=x+vertex.x;
			rv.y=y+vertex.y;
			rv.z=z+vertex.z;
			rv.w=1;
			return rv;
		}else{
			say("加法参数错误");
		}
	}
	/**  转换函数
	*
	* @usage
	* @return  转换成Vertex
	*/
	public static function Parse () : Vertex
	{
		if (arguments.length > 1)
		{
		} else
		{
			if (arguments.length == 0 || arguments == undefined)
			{
				return (new Vertex (0, 0, 0, 1));
			}
			var p = arguments [0];
			switch (typeof (p))
			{
				case "array" :
				if (p.length < 1)
				{
					say ("转换不成功 array.length<=0");
					return null;
				}
				var vertex : Vertex = new Vertex (Number (p [0]) , Number (p [1]) , Number (p [2]) , Number (p [3]));
				return vertex;
				break;
				case "object" :
				var a : Array = new Array ();
				for (var i in p)
				{
					if (i.indexOf ("x") != - 1)
					{
						a [0] = Number (p [i]);
					}
					if (i.indexOf ("y") != - 1)
					{
						a [1] = Number (p [i]);
					}
					if (i.indexOf ("z") != - 1)
					{
						a [2] = Number (p [i]);
					}
					if (i.indexOf ("w") != - 1)
					{
						a [3] = Number (p [i]);
					}
				}
				if (a.length <= 0)
				{
					if (p.length <= 0)
					{
						say ("转换不成功 obj中没有x|y|z|w属性");
						return null;
					} else
					{
						var vertex : Vertex = new Vertex (Number (p [0]) , Number (p [1]) , Number (p [2]) , Number (p [3]));
						return vertex;
					}
				} else
				{
					return (Vertex.Parse (a));
				}
				break;
				case "string" :
				if (p.indexOf (",") != - 1)
				{
					var a : Array = new Array ();
					a = p.split (",");
					var vertex : Vertex = new Vertex (Number (a [0]) , Number (a [1]) , Number (a [2]) , Number (a [3]));
					return vertex;
				} else
				{
					var pp = eval (p);
					return (Vertex.Parse (pp));
				}
				break;
			}
		}
	}
	/** 输出点坐标数据
	*
	* @usage
	* @return
	*/
	public function toString ()
	{
		say ("(" + x + "," + y + "," + z + "," + w + ")");
	}
	/** 输出函数
	*
	* @usage
	* @param   o
	* @return
	*/
	private static function say (o)
	{
		trace ("vertex:" + o);
	}
}
